/**
 * Copyright &copy; 2020 <a href="https://github.com/somowhere/exam-system">exam-system</a> All rights reserved.
 */
package com.shanhesoft.java.modules.exam.web;


import lombok.extern.slf4j.Slf4j;
import io.swagger.annotations.ApiOperation;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shanhesoft.java.common.core.constant.CommonConstants;
import com.shanhesoft.java.common.core.util.Result;
import com.shanhesoft.java.common.core.vo.PageModel;
import com.shanhesoft.java.common.data.util.QueryWrapperUtil;
import com.shanhesoft.java.common.log.annotation.LogOperate;
import com.shanhesoft.java.common.log.enums.BusinessType;
import com.shanhesoft.java.common.web.resource.BaseResource;
import com.shanhesoft.java.modules.exam.domain.PaperQue;
import com.shanhesoft.java.modules.exam.domain.dto.ExaminationRecordDto;
import com.shanhesoft.java.modules.exam.domain.dto.PaperQueDto;
import com.shanhesoft.java.modules.exam.domain.dto.PaperQueQueryCriteria;
import com.shanhesoft.java.modules.exam.service.*;

import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import lombok.AllArgsConstructor;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * 试卷试题管理Controller 试卷试题管理
 * @author admin
 * @version 2020-09-26 15:53:22
 */
@RestController
@RequestMapping(value = "${application.admin-path}/exam/paperQue")
@Slf4j
@AllArgsConstructor
public class PaperQueResource extends BaseResource {

	private final PaperQueService service;

	private final ExaminationRecordService examinationRecordService;
	private final TBankFillQueService fillQueService;
	private final TBankJudgeQueService judgeQueService;
	private final TBankSingleChoiceQueService singleChoiceQueService;
	private final TBankMultipleChoiceQueService multipleChoiceQueService;
	private final OjProblemsService ojProblemsService;


	private final PaperQueService paperQueService;

	/**
	 * @param id
	 * @return
	 */
	@GetMapping(CommonConstants.URL_ID_REGEX)
	@PreAuthorize("@pms.hasPermission('exam_paperQue_view')")
	public Result get(@PathVariable String id) {
		log.debug("REST request to get Entity : {}", id);
		return  Result.buildOkData(service.getOneDto(id));
	}

	/**
	 * GET / : 题库题库查询
	 *
	 * @return the Result with status 200 (OK) and with body all tQuestionBank
	 */

	@GetMapping("/getQueByCondtions")
	@ApiOperation(value = "根据条件查询题目信息")
	public Result getQueByCondtions(String examRecordId, String queId, String queType) {
		ExaminationRecordDto examinationRecordDto = examinationRecordService.getOneDto(examRecordId);
		if(null == examinationRecordDto) {
			return  Result.buildFail("考试记录不存在");
		}

		Map<String, Object> map = new HashMap<>();

		// 获取分数
		QueryWrapper<PaperQue> paperQueQueryWrapper = new QueryWrapper<PaperQue>();
		paperQueQueryWrapper.eq("paper_id", examinationRecordDto.getPaperId());
		paperQueQueryWrapper.eq("que_id", queId);
		paperQueQueryWrapper.eq("que_type", queType);
		PaperQue paperQue = paperQueService.getOne(paperQueQueryWrapper);
		map.put("queScore", paperQue.getScore());

		if("0".equals(queType)) {
			map.put("que", singleChoiceQueService.getOneDto(queId));
			return Result.buildOkData(map);
		} else if("1".equals(queType)) {
			map.put("que", multipleChoiceQueService.getOneDto(queId));
			return Result.buildOkData(map);
		} else if("2".equals(queType)) {
			map.put("que", judgeQueService.getOneDto(queId));
			return Result.buildOkData(map);
		} else if ("3".equals(queType)) {
			map.put("que", fillQueService.getOneDto(queId));
			return Result.buildOkData(map);
		} else if ("4".equals(queType)) {
			map.put("que", ojProblemsService.getOneDto(queId));
			return Result.buildOkData(map);
		} else {
			return Result.buildFail("不支持的查询题目类型。");
		}
	}
	/**
	 * GET / : get all paperQue.
	 *
	 * @param pm the pagination information
	 * @return the Result with status 200 (OK) and with body all paperQue
	 */

	@PreAuthorize("@pms.hasPermission('exam_paperQue_view')")
	@GetMapping
	@ApiOperation(value = "试卷试题管理查看")
	public Result getPage(PageModel pm, PaperQueQueryCriteria paperQueQueryCriteria) {
		QueryWrapper wrapper = QueryWrapperUtil.getWrapper(pm, paperQueQueryCriteria);
		return Result.buildOkData(service.page(pm, wrapper));
	}

	/**
	 * POST / : Save a paperQueDto.
	 *
	 * @param paperQueDto the HTTP paperQue
	 */
	@PreAuthorize("@pms.hasPermission('exam_paperQue_edit')")
	@ApiOperation(value = "试卷试题管理编辑")
	@PostMapping
	public Result save(@Valid @RequestBody PaperQueDto paperQueDto) {
		log.debug("REST request to save PaperQueDto : {}", paperQueDto);
		service.saveOrUpdate(paperQueDto);
		return Result.buildOk("保存试卷试题管理成功");

	}

	/**
	 * DELETE //:ids : delete the "ids" PaperQue.
	 *
	 * @param ids the id of the paperQue to delete
	 * @return the Result with status 200 (OK)
	 */
	@PreAuthorize("@pms.hasPermission('exam_paperQue_del')")
	@ApiOperation(value = "试卷试题管理删除")
	@DeleteMapping
	public Result delete(@RequestBody Set<String> ids) {
		log.debug("REST request to delete PaperQue: {}", ids);
		service.removeByIds(ids);
		return Result.buildOk("删除试卷试题管理成功");
	}
}
